//
//  WebViC.m
//  LoveTourism
//
//  Created by qianfeng0 on 16/6/24.
//  Copyright © 2016年 王超. All rights reserved.
//

#import "WebVC.h"
#import "FunCollectionManager.h"
#import "LXAlertView.h"

@interface WebVC ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation WebVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self LoadingWebView];
    
    [self creatButton];
    
}

- (void)LoadingWebView {
    
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.str]];
    [_webView loadRequest:request];
    
    
}

- (void)creatButton {
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:@"iconfont-iconfontshoucang"] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(collection:) forControlEvents:UIControlEventTouchUpInside];
    
    //将button设置为导航栏的右按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
}

- (void)collection:(UIButton *)button {
    
    button.selected = !button.isSelected;
    
    if (button.isSelected) {
        //是否已经收藏
        BOOL result = [[FunCollectionManager shareManager] favoriateIsExist:self.model];
        if (result) {
            LXAlertView * alertView = [[LXAlertView alloc] initWithTitle:@"温馨提示" message:@"已经收藏过" cancelBtnTitle:nil otherBtnTitle:@"确定" clickIndexBlock:nil];
            
            alertView.animationStyle = LXASAnimationTopShake;
            [alertView showLXAlertView];
        } else {
            
            LXAlertView * alertView2 = [[LXAlertView alloc] initWithTitle:@"温馨提示" message:@"是否收藏" cancelBtnTitle:@"否" otherBtnTitle:@"是" clickIndexBlock:^(NSInteger clickIndex) {
                //取消按钮
                if (clickIndex == 0) {
                    
                } else {//确定
                    //添加
                    BOOL res = [[FunCollectionManager shareManager] addFavorite:self.model];
                    if (res) {
                        
                        [button setImage:[UIImage imageNamed:@"iconfont-iconfontshoucang-2"] forState:UIControlStateNormal];
                        LXAlertView * alert = [[LXAlertView alloc] initWithTitle:@"温馨提示" message:@"收藏成功" cancelBtnTitle:nil otherBtnTitle:@"确定" clickIndexBlock:nil];
                        alert.animationStyle = LXASAnimationLeftShake;
                        [alert showLXAlertView];
                    } else {
                        LXAlertView * alert = [[LXAlertView alloc] initWithTitle:@"温馨提示" message:@"收藏失败" cancelBtnTitle:nil otherBtnTitle:@"确定" clickIndexBlock:nil];
                        alert.animationStyle = LXASAnimationNO;
                        [alert showLXAlertView];
                    }//res
                }//下标
            }];
            [alertView2 showLXAlertView];
        }//result
        
    } else {//isSelected
        
        [button setImage:[UIImage imageNamed:@"iconfont-iconfontshoucang"] forState:UIControlStateNormal];
    }
}

@end





























